const options = {
  state() {
    return {
      examType: '',
      examData: [],
      examFavs: [],
      examIndex: 0,
      correctNum: 0,
      wrongNum: 0,
    }
  },
  mutations: {
    updateType(state, newType) {
      state.examType = newType
    },
    updateData(state, newData) {
      state.examData = newData
    },
    updateDataElement(state, newElement) {
      state.examData[newElement.index][newElement.key] = newElement.value
    },
    updateFavs(state, newFavs) {
      state.examFavs = newFavs
    },
    updateIndex(state, newIndex) {
      state.examIndex = newIndex
    },
    // 在vue2环境下的微信小程序中使用getters计算未达成预期效果，故只能手动统计对错结果
    updateNum(state) {
      const correctList = state.examData.filter((it) => it.correct && it.submit)
      const wrongList = state.examData.filter((it) => !it.correct && it.submit)
      state.correctNum = correctList.length
      state.wrongNum = wrongList.length
    },
  }
}

// #ifdef VUE2
import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);
const store = new Vuex.Store(options);
// #endif

// #ifdef VUE3
import {
  createStore
} from "vuex";
const store = createStore(options);
// #endif

export default store;